package com.github.aloxc.plugin.restplus.test;


import com.github.aloxc.plugin.restplus.entity.Setting;
import com.github.aloxc.plugin.restplus.method.HttpMethod;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
/**
 * @author leero
 */
public class RestRequest {
    private HttpMethod httpMethod;
    private String url;
    private String service;
    private Collection<Setting> headerList;
    private Collection<Setting> cookieList;
    private Collection<Setting> parameters;
    private String script;
    private String body;
    private List<RestRequestListener> listenerList = new ArrayList<>();

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getScript() {
        return script;
    }

    public void setScript(String script) {
        this.script = script;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Collection<Setting> getHeaderList() {
        return headerList;
    }

    public void setHeaderList(Collection<Setting> headerList) {
        this.headerList = headerList;
    }

    public Collection<Setting> getCookieList() {
        return cookieList;
    }

    public void setCookieList(Collection<Setting> cookieList) {
        this.cookieList = cookieList;
    }

    public Collection<Setting> getParameters() {
        return parameters;
    }

    public void setParameters(Collection<Setting> parameters) {
        this.parameters = parameters;
    }

    public HttpMethod getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(HttpMethod httpMethod) {
        this.httpMethod = httpMethod;
    }

    public void deleteHeader(@NotNull String name) {
        this.headerList.removeIf(header -> name.equals(header.getName()));
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    public String createQueryString() {
        return StringUtil.join(this.parameters, pair -> {
            try {
                String key = URLEncoder.encode(pair.getName(), "UTF-8");
                String value = URLEncoder.encode(pair.getValue(), "UTF-8");
                return key + "=" + value;
            } catch (UnsupportedEncodingException e) {

                return "";
            }
        },"&");
    }



    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        RestRequest request = (RestRequest) o;

        if (!Objects.equals(this.headerList, request.headerList)) {
            return false;
        }
        if (!Objects.equals(this.httpMethod, request.httpMethod)) {
            return false;
        }
        if (!Objects.equals(this.parameters, request.parameters)) {
            return false;
        }
        return true;
    }


    @Override
    public int hashCode() {
        int result = Objects.hashCode(this.httpMethod);
        result = 31 * result + Objects.hashCode(this.headerList);
        result = 31 * result + Objects.hashCode(this.parameters);
        return result;
    }
    public void addRequetListener(RestRequestListener listener){
        listenerList.add(listener);
    }

    public List<RestRequestListener> getListenerList() {
        return listenerList;
    }
}